import unittest

import HTMLTestRunner

from haha.TinyShop.TS_Common import common


class addtestcase(unittest.TestCase):
    def setUp(self):
        print("start...")
        self.add=common.nameadd()
    def test_case1(self):
        print("testcase1")
        # 所有参数正确
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn('上架',re)
    def test_case2(self):
        print("testcase2")
        #缺少分类
        re=self.add.add({'category_id':'','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("选择分类，若无分类请先创建",re)

    def test_case3(self):
        print("testcase3")
        #缺少商品名称
        re=self.add.add({'category_id':'5','name':'','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("不能为空",re)

    def test_case4(self):
        print("testcase4")
        #缺少商品编号
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("请输入3个以上的字符(不能为中文)",re)

    def test_case5(self):
        print("testcase5")
        #缺少图片
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'',
                     'img':'',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("添加商品图片",re)

    def test_case6(self):
        print("testcase6")
        #缺少货号
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("请输入3个以上的字符(不能为中文)",re)

    def test_case7(self):
        print("testcase7")
        #缺少库存
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("必需为整数",re)

    def test_case8(self):
        print("testcase8")
        #缺少预警线
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("必需为整数",re)

    def test_case9(self):
        print("testcase9")
        #缺少重量
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'30','weight':''})
        self.assertIn("必需为整数",re)

    def test_case10(self):
        print("testcase10")
        #缺少零售价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'',
                     'market_price':'120','cost_price':'30','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case11(self):
        print("testcase11")
        #缺少市场价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'','cost_price':'30','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case12(self):
        print("testcase12")
        #缺少市场价
        re=self.add.add({'category_id':'5','name':'衬衫','goods_no':'t001',
                     'point':'0','sort':'1','unit':'件',
                     'imgs':'data/uploads/2014/05/04/ef5a1ccdd16137f07139fe972f8be244.jpg',
                     'img':'data/uploads/2014/05/04/6a63f7365e4430b5d07791fd32c66500.jpg',
                     'pro_no':'t001_1','store_nums':'10','warning_line':'2','sell_price':'100',
                     'market_price':'120','cost_price':'','weight':'500'})
        self.assertIn("整数或保留小数点后两位精确度的数",re)

    def test_case13(self):
        print("testcase13")
        #所有参数为空
        re=self.add.add({'category_id':'','name':'','goods_no':'',
                     'point':'','sort':'','unit':'',
                     'imgs':'',
                     'img':'',
                     'pro_no':'','store_nums':'','warning_line':'','sell_price':'',
                     'market_price':'','cost_price':'','weight':''})
        self.assertIn("整数或保留小数点后两位精确度的数",re)
    def tearDown(self):
        print('end...')


if __name__ == "__main__":
    su=unittest.TestSuite(unittest.TestLoader().loadTestsFromTestCase(addtestcase))
    f=open('../TS_TestRunner/result.html','wb')
    r=HTMLTestRunner.HTMLTestRunner(
        stream=f,
        title='TinyShop新增商品中心',
        description='TinyShop新增商品中心测试报告'
    )
    r.run(su)
    f.close()